/**
 * @Project : ProgrammingExercises
 * @Package : Leetcode
 * @File : Q2541.java
 * @Author : WangRuoyu
 * @Date 2023/1/29 11:03
 */

package leetcode;

public class Q2541 {
    public long minOperations(int[] nums1, int[] nums2, int k) {
        if (k == 0) {
            for (int i = 0; i < nums1.length; i++) {
                if (nums1[i] != nums2[i]) {
                    return -1;
                }
            }
            return 0;
        } else {
            long res = 0;
            long resn = 0;
            long tmp = 0;
            long tmp2 = 0;

            for (int i = 0; i < nums1.length; i++) {
                tmp = nums1[i] - nums2[i];
                if ((tmp % k) == 0) {
                    tmp2 = tmp / k;
                    if (tmp > 0) {
                        res += tmp2;
                    } else if (tmp < 0) {
                        resn -= tmp2;
                    }
                } else {
                    return -1;
                }
            }
            if (res == resn) {
                return res;
            }
            return -1;
        }
    }

    public static void main(String[] args) {
        Q2541 sl = new Q2541();
//        int[] nums1 = {4, 3, 1, 4};
//        int[] nums2 = {1, 3, 7, 1};
        int[] nums1 = {1, 2, 3, 4};
        int[] nums2 = {1, 2, 3, 4};
        int k = 0;
        System.out.println(sl.minOperations(nums1, nums2, k));
    }
}